#!/usr/bin/python
#coding: utf-8

class Person:
    '''Represents a person.'''
    population = 0
    def __init__(self, name):
        self.name = name
        print '(Initializing %s)' % self.name

        #When this person is created, he/she adds to the population
        Person.population += 1

    def __del__(self):
        '''I am dying.'''
        print '%s says bye.' % self.name

        Person.population 
        Person.population -= 1

        if Person.population == 0:
            print 'I am the last one.'
        else:
            print 'There are still %d people left.' % Person.population

    def sayHi(self):
        print 'Hi, my name is %s.' % self.name

    def howMany(self):
        '''prints the current population.'''
        if Person.population == 1:
            print 'I am the only person here.'
        else:
            print 'We have %d persons here.' % Person.population




larry = Person('Larry')
larry.sayHi()
larry.howMany()

mary = Person('Mary')
mary.sayHi()
mary.howMany()

print '=============================='

#del在python销毁运行时环境时是会自动调用到到，但是，在python自动销毁到时候，调用顺序是先销毁Person类，在销毁其实例，这时就会出现如下异常：
#Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb781e7ac>> ignored
#这是因为Person类被销毁后，其类变量population也不存在了
del(larry)
del(mary)
